package knowse.app.daos;

import knowse.app.utils.Helper;
import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class ChangeDAO {
	private final String TAG = "ChangeDAO";
	private final String ERRORMSG = "Insertion Failed";
	
	protected static final String TABLE = "Change"; 
	
	//primary key
	protected static final String ChangeID = "ChangeID"; 
	
	protected static final String JsonClass = "JsonClass"; 
	protected static final String FileDir = "FileDir";
	protected static final String FileName = "FileName";
	protected static final String IsTextFile = "IsTextFile";
	protected static final String IsBinaryFile = "IsBinaryFile";
	protected static final String NumChangedLines = "NumChangedLines";	
	protected static final String NumAddedLines = "NumAddedLines";
	protected static final String NumDeletedLines = "NumDeletedLines";
	protected static final String NumBytesBefore = "NumBytesBefore";
	protected static final String NumBytesAfter = "NumBytesAfter";

	//Foreign key
	protected static final String CommitID = "CommitID"; 
	
	
	public ChangeDAO(){
	}
	
	//insert methods to retrieve and update the database
	
	/*
	 * TODO
	 * Store TestStatusDataObject to the database
	 * 
	 */
	public void store(ChangeDataObject tdo, int commitID) throws Exception{
	
		ContentValues values= new ContentValues();
		values.put(JsonClass, tdo.getJsonClass());
		values.put(FileDir, tdo.getFileDir());
		values.put(FileName, tdo.getFileName());
		values.put(IsTextFile, Helper.getInstance().fromBooleanToInteger(tdo.isTextFile()));
		values.put(IsBinaryFile, Helper.getInstance().fromBooleanToInteger(tdo.isBinaryFile()));
		values.put(NumChangedLines, tdo.getNumChangedLines());
		values.put(NumAddedLines, tdo.getNumAddedLines());
		values.put(NumDeletedLines, tdo.getNumDeletedLines());
		values.put(NumBytesBefore, tdo.getNumBytesBefore());
		values.put(NumBytesAfter, tdo.getNumBytesAfter());
		
		values.put(CommitID, commitID);
		
		DBHelper dbHelper= new DBHelper();
		SQLiteDatabase db= dbHelper.getWritableDatabase();
		Long insertResult = db.insert(TABLE, null, values);
		if(insertResult < 0){
			throw new Exception(TAG + " " + ERRORMSG);
		}
		Log.i(TAG, insertResult.toString());
		db.close();
	}
	
}

